package com.situ.springboot.controller;

import com.situ.springboot.pojo.User;
import com.situ.springboot.pojo.query.CommentQuery;
import com.situ.springboot.service.ICommentService;
import com.situ.springboot.util.PageResult;
import com.situ.springboot.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/comment")
public class CommentController {
    //注入对象使用
    //@Autowired
    //private CommentMapper commentMapper;
    @Autowired
    private ICommentService commentService;


    // /comment/list?page=1&limit=10&name=zhangsan&email=233@qq.com
    @RequestMapping("/list")
    @ResponseBody   //以json形式返回数据
    public PageResult list(CommentQuery commentQuery, HttpSession session) {
        System.out.println("CommentController.list");
        //获得已经登录的用户信息
        Object token = session.getAttribute("token");
        if (token instanceof User) {
            User user = (User) token;
            commentQuery.setUserId(user.getId());
        }
        PageResult pageResult = commentService.list(commentQuery);
        return pageResult;
    }

    //转发页面，返回String，不需要加@ResponseBody
    @RequestMapping("/toCommentList")
    public String toCommentList() {
        return "comment_list";
    }

    @RequestMapping("/deleteById")
    @ResponseBody
    public Result deleteById(Integer id) {
        commentService.deleteById(id);
        return Result.ok("删除成功");
    }

    @RequestMapping("/deleteAll")
    @ResponseBody
    public Result deleteAll(Integer[] ids) {
        //Alt+Enter
        commentService.deleteAll(ids);
        return Result.ok("删除成功");
    }
}
